


<?php
class RbacAction extends ComAction{
   
   public function AddRole(){
   
   	$this->display();
   }

  public function Index(){
  	import('ORG.Util.Page');
  	$count=M('user')->count();
  	$page=new Page($page,10);
  	$limit=$page->firstRow.','.$page->listRows;
   
  	//$this->user=M('user')->limit($limit)->select();
   $this->user=D('UserRelation')->relation(true)->limit($limit)->select();

  	$this->page=$page->show();
  	$this->display();
  }
  
   public function AddRoleRun(){
	   	$name=$_POST['name'];
	   	$remark=$_POST['remark'];
	   	$data=array(
	        'name'=>$name,
	        'remark'=>$remark,
	        'status'=>$_POST['status']
	   		);
	   if(M('role')->add($data)){
	   	  $this->success('添加角色成功！');
	   }else{
	   	   $this->success('添加角色失败！');
	   }
   }
   public function AddUser(){
   	$field=array('id','name','remark');
   	$this->role=M('role')->field($field)->select();

   	$this->display();
   }

   public function DelUser(){
   	$where=(array('id'=>$_GET['uid']));
      if(M('user')->where($where)->delete()){
          
          if(M('role_user')->where(array('user_id'=>$_GET['uid']))->delete()){
          	$this->success('删除用户成功！');
          }else{
          	$this->success('删除用户失败！');
          }

      }else{
            $this->success('删除用户失败！');
      }

   }

   public function AddUserRun(){
      
      $username=$_POST['username'];
      $data=array(
      	'username'=>$username,
      	'password'=>md5($_POST['psw']),
      	'logintime'=>time(),
      	'loginip'=>get_client_ip()
      	);
      if($uid=M('user')->add($data)){
      	 $data=array();
      	 foreach(I('role_id') as $v){
            $data[]=array('role_id'=>$v,'user_id'=>$uid);
      	 }
      	 if(M('role_user')->addAll($data)){
      	 	$this->success('添加用户成功！');
      	 }
      }else{
      	$this->error('添加用户失败！');
      }

     
   }

   public function AddNode(){
    
    switch ($_GET['level']) {
    	case 1:
    		$type="应用";
    		break;   	
    	case 2:
    		$type="控制器";
    		break;
    	case 3:
    		$type="方法";
    		break;
    }
    $this->type=$type;
    $this->pid= (int) $_GET['pid'];
    $this->level= (int) $_GET['level'];
   	$this->display();
   }
   
   public function AddNodeRun(){
 	$pid=$_POST['pid'];
    $level=$_POST['level'];
    $name=$_POST['name'];
    $title=$_POST['title'];
    $sort=$_POST['sort'];
    $status=$_POST['status'];
   
    $data=array(
        'name'=>$name,
        'level'=>$level,
        'pid'=>$pid,
        'title'=>$title,
        'status'=>$status,
        'sort'=>$sort,
    	);
   if(M('node')->add($data)){
      $this->success('添加用户成功！');
   }else{
      $this->error('添加用户失败！');
   }

   }
   

   public function Node(){

   	$node=M('node')->order('sort')->select();
   	import('Class.Category',APP_PATH);
     $this->node=Category::UnlimiteForLayer($node);
   //p($node);die;
   	$this->display();
   }


    public function Role(){

   	 import('ORG.Util.Page');
    $count=M('role')->count();
   	$page=new Page($count,10);
    $limit=$page->firstRow .','.$page->listRows;
    
   	$this->role= M('role')->limit($limit)->select();
   	$this->page=$page->show();
   	$this->display();
   }
   
   public function DelRole(){
       
       if(M('role')->where(array('id'=>$_GET['rid']))->delete()){
          
          if(M('access')->where(array('role_id'=>$_GET['rid']))->delete()){
          	$this->success('删除角色成功！');
          }else{
          	$this->success('删除角色失败！');
          }

      }else{
            $this->success('删除角色失败！');
      }

   }
   
   public function Access(){
   	import('Class.Category',APP_PATH);
    $node=M('node')->order('sort')->select();
    $rid=I('rid',0,'intval');
    $this->rid=$rid;
    $access=M('access')->where(array('role_id'=>$rid))->getField('node_id',true);
    $this->node=node_mergy($node,$access);
    
   	$this->display();
   }

   public function  SetAccess(){
      $rid=I('rid');
      $access=I('access');
      $data=array();
      M('access')->where(array('role_id'=>$rid))->delete();
      foreach ($access as $v) {
      	 $tmp=explode("_", $v);
      	 $data[]=array('role_id'=>$rid,'node_id'=>$tmp[0],'level'=>$tmp[1]); 	 
      }
    
      if(M('access')->addAll($data)){
      	$this->success('权限保存成功！');
      }else{
      	$this->success('权限保存失败！');
      }
   }

}


?>